package com.zhang.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;

@Configuration
public class GlobalCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
//@Configuration
//public class GulimallCorsConfiguration {
//
//    @Bean
//    public CorsWebFilter corsWebFilter(){
//        // 基于URL的跨域配置源对象
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        // 创建跨域配置
//        CorsConfiguration corsConfiguration = new CorsConfiguration();
//
//        //1、配置跨域
//        corsConfiguration.addAllowedHeader("*");// 任意的请求头
//        corsConfiguration.addAllowedMethod("*");// 任意类型的请求方法
//        corsConfiguration.addAllowedOrigin("*");// 允许任意来源的请求：旧版本
////        corsConfiguration.addAllowedOriginPattern("*");// 允许任意来源的请求：新版本
//        corsConfiguration.setAllowCredentials(true);// 允许cookie进行跨域，否则跨域将会丢失cookie
//        // 跨域配置源对象注入跨域配置：该跨域配置源处理的路径是所有的接口
//        source.registerCorsConfiguration("/**",corsConfiguration);// 任何的请求接口都要进行跨域处理
//        return new CorsWebFilter(source);
//    }
//}

